package com.infomanage.service.plan;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.jsp.JspWriter;

import com.infomanage.authority.AuthorityBean;
import com.infomanage.bean.FieldBean;
import com.infomanage.bean.PaginationBean;
import com.infomanage.bean.SearchBean;
import com.infomanage.dao.AffairmanagePlanDAO;
import com.infomanage.enums.RoleType;
import com.infomanage.po.AffairmanagePlan;
import com.infomanage.po.User;
import com.infomanage.util.Consts;
import com.infomanage.util.StringUtil;
import com.infomanage.vo.AffairmanagePlanItem;
import com.jiuqi.util.ReturnObject;
import com.jiuqi.util.StringUtils;

/**
 * 内务管理计划
 *
 */
public class AffairManagePlanService {

	public static AffairmanagePlanItem loadAffairManagePlan(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			request.setCharacterEncoding("UTF-8");
			String planId= request.getParameter("affairId");
			if(StringUtils.isEmpty(planId)) return null;
			AffairmanagePlanDAO dao = new AffairmanagePlanDAO();
			return dao.find(Integer.parseInt(planId));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static List<AffairmanagePlanItem> loadAffairManagePlanList(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			request.setCharacterEncoding("UTF-8");
			AffairmanagePlanDAO dao = new AffairmanagePlanDAO();
			List<AffairmanagePlanItem> list = dao.findAll();
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static ReturnObject saveAffairManagePlan(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			AffairmanagePlan plan = new AffairmanagePlan();
			plan.setCheckObject(request.getParameter("checkObject"));
			plan.setContent(request.getParameter("content"));
			plan.setDeptId(Integer.parseInt(request.getParameter("deptId")));
			plan.setDescription(request.getParameter("description"));
			plan.setExcuteTime(request.getParameter("executeTime"));
			plan.setPersonId(Integer.parseInt(request.getParameter("personId")));
			AffairmanagePlanDAO dao = new AffairmanagePlanDAO();
			dao.add(plan);
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(StringUtil.filterIllegalchar(e.getMessage()));
		}
		return ret;
	}
	
	public static ReturnObject modifyAffairManagePlan(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			int planId = Integer.parseInt(request.getParameter("affairId"));
			AffairmanagePlanDAO dao = new AffairmanagePlanDAO();
			AffairmanagePlan plan = dao.find(AffairmanagePlan.class, planId);
			plan.setCheckObject(request.getParameter("checkObject"));
			plan.setContent(request.getParameter("content"));
			plan.setDeptId(Integer.parseInt(request.getParameter("deptId")));
			plan.setDescription(request.getParameter("description"));
			plan.setExcuteTime(request.getParameter("executeTime"));
			plan.setPersonId(Integer.parseInt(request.getParameter("personId")));
			dao.modify(plan);
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(StringUtil.filterIllegalchar(e.getMessage()));
		}
		return ret;
	}
	
	public static ReturnObject removeAffairManagePlan(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String planId = request.getParameter("affairId");
			AffairmanagePlanDAO dao = new AffairmanagePlanDAO();
			dao.remove(AffairmanagePlan.class, Integer.parseInt(planId));
		} catch (Exception e) {
			ret.SetErrored(true);
			ret.setErrorMessage(e.getMessage());
		}
		return ret;
	}
	
	public static List<AffairmanagePlanItem> search(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			request.setCharacterEncoding("UTF-8");
			AffairmanagePlanDAO dao = new AffairmanagePlanDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			//设置分页
			String currPageNum = request.getParameter("currPageNum");
			PaginationBean page = new PaginationBean();
			if(currPageNum == null){
				page.setCurrentPageNum(1);
			}else{
				page.setCurrentPageNum(Integer.parseInt(currPageNum));
				
			}
			searchBean.setPage(page);
			List<AffairmanagePlanItem> list = dao.findByFields(searchBean);
			return list;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static ReturnObject removeBatchAffairManagePlan(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		ReturnObject ret = new ReturnObject();
		try {
			request.setCharacterEncoding("UTF-8");
			String batchIds = request.getParameter("batchIds");
			AffairmanagePlanDAO dao = new AffairmanagePlanDAO();
			if(batchIds != null){
				String[] ids = batchIds.split(";");
				for(String id : ids){
					dao.remove(AffairmanagePlan.class, Integer.parseInt(id));
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			ret.SetErrored(true);
			ret.setErrorMessage("删除失败！");
		}
		return ret;
	}
	
	public static int getPageCount(HttpServletRequest request, HttpServletResponse response, JspWriter out){
		try {
			AffairmanagePlanDAO dao = new AffairmanagePlanDAO();
			SearchBean searchBean = buildSearchCondition(request, response, out);
			return (dao.count(searchBean) + PaginationBean.pageSize - 1)/PaginationBean.pageSize;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 1;
	}
	
	private static SearchBean buildSearchCondition(HttpServletRequest request, HttpServletResponse response, JspWriter out) throws Exception{
		request.setCharacterEncoding("UTF-8");
		String deptId = request.getParameter("deptId");
		String personId = request.getParameter("personId");
		String excuteTime = request.getParameter("excuteTime");
		SearchBean searchBean = new SearchBean();
		if(!StringUtil.isEmpty(deptId)){
			FieldBean field = new FieldBean("deptId", Integer.parseInt(deptId));
			searchBean.add(field);
		}
		if(!StringUtil.isEmpty(personId)){
			FieldBean field = new FieldBean("personId", Integer.parseInt(personId));
			searchBean.add(field);
		}
		if(!StringUtil.isEmpty(excuteTime)){
			FieldBean field = new FieldBean("excuteTime", excuteTime);
			searchBean.add(field);
		}
		//设置权限
		User user = (User) request.getSession().getAttribute(Consts.LOGINED_USER);
		if (user != null) {
			AuthorityBean authority = new AuthorityBean();
			authority.setRole(RoleType.valueOf(user.getRoleId()));
			authority.setDeptId(user.getAssociatedDept());
			searchBean.setAuthority(authority);
		}
	return searchBean;
}
}
